package com.laihui.fragment;

import android.app.Activity;
import android.util.Log;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.laihui.Constants;
import com.laihui.MyApplication;
import com.laihui.activity.R;
import com.laihui.base.BaseFragment;
import com.laihui.entity.LoanBean;
import com.laihui.entity.LoanList;
import com.laihui.model.InvestDetail;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;


public class LoanPicFragment extends BaseFragment {



    @BindView(R.id.ll_container)
    LinearLayout mLlContainer;
    private String css;
    private String con;

    private Activity mActivity;
    private MyApplication application;
    LoanBean loan;


    @Override
    public void init() {
        mActivity = getActivity();

//        application = (MyApplication) getApplication();
//        con = getIntent().getStringExtra("con");


        loan = (LoanBean) getArguments().getSerializable("data");
        Log.e("loannull",loan==null?"1":"0");
        Log.e("loan.pics null",loan.pics==null?"1":"0");
        List<LoanBean.LoanImg> imgs = loan.pics;
        for (int i=0;i<imgs.size();i++){
            ImageView iv = new ImageView(getContext());
            Glide.with(this).load(Constants.BASE_URL_IMG + imgs.get(i)).into(iv);

            mLlContainer.addView(iv);
        }

        //if (imgs==null){
      //  webView.loadDataWithBaseURL(null, "<html><header>" + css + "</header><body>" + replaceHtmlImgToAbsolutelyUrl(Constants.BASE_URL_IMG+"/",investDetail.getImgs()) + "</body></html>", "text/html", "UTF-8", null);

        //webView.loadDataWithBaseURL(null, "<html><header>" + css + "</header><body>" +replaceHtmlImgToAbsolutelyUrl(Constants.BASE_URL_CONTENT+"/",con) + "</body></html>", "text/html", "UTF-8", null);
    }

    @Override
    public int getFragmentId() {
        return R.layout.fragment_loan_base_info;
    }

    @Override
    public Object bindFragment() {
        return this;
    }

    public static String replaceHtmlImgToAbsolutelyUrl(String baseUrl, String html){
        Pattern pattern = Pattern.compile("src\\s*=\\s*\"(.+?)\"");
        Matcher matcher = pattern.matcher(html);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            String str = matcher.group(0);
            if (!str.contains("http://")){
                matcher.appendReplacement(sb,str.substring(0,5)+baseUrl+str.substring(6));
            }
        }
        matcher.appendTail(sb);
        return sb.toString();
    }


}
